using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SheLife.Application.Interfaces;
using SheLife.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SheLife.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [Authorize]
    public class SkincareAnalysisController : ControllerBase
    {
        private readonly ISkincareAnalysisService _skincareAnalysisService;

        public SkincareAnalysisController(ISkincareAnalysisService skincareAnalysisService)
        {
            _skincareAnalysisService = skincareAnalysisService;
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<SkincareAnalysis>> GetSkincareAnalysis(int id)
        {
            var analysis = await _skincareAnalysisService.GetSkincareAnalysisByIdAsync(id);
            if (analysis == null)
            {
                return NotFound();
            }
            return analysis;
        }

        [HttpGet("user/{userId}")]
        public async Task<ActionResult<IEnumerable<SkincareAnalysis>>> GetSkincareAnalysesByUserId(int userId)
        {
            var analyses = await _skincareAnalysisService.GetSkincareAnalysesByUserIdAsync(userId);
            return Ok(analyses);
        }

        [HttpGet("user/{userId}/daterange")]
        public async Task<ActionResult<IEnumerable<SkincareAnalysis>>> GetSkincareAnalysesByDateRange(
            int userId, 
            [FromQuery] DateTime startDate, 
            [FromQuery] DateTime endDate)
        {
            var analyses = await _skincareAnalysisService.GetSkincareAnalysesByDateRangeAsync(userId, startDate, endDate);
            return Ok(analyses);
        }

        [HttpGet("user/{userId}/latest")]
        public async Task<ActionResult<SkincareAnalysis>> GetLatestSkincareAnalysis(int userId)
        {
            var analysis = await _skincareAnalysisService.GetLatestSkincareAnalysisAsync(userId);
            if (analysis == null)
            {
                return NotFound();
            }
            return Ok(analysis);
        }

        [HttpGet("user/{userId}/skintypetrend")]
        public async Task<ActionResult<object>> GetSkinTypeTrend(int userId)
        {
            var trend = await _skincareAnalysisService.GetSkinTypeTrendAsync(userId);
            return Ok(trend);
        }

        [HttpGet("user/{userId}/concerns")]
        public async Task<ActionResult<object>> GetSkinConcerns(int userId)
        {
            var concerns = await _skincareAnalysisService.GetSkinConcernsAsync(userId);
            return Ok(concerns);
        }

        [HttpPost]
        public async Task<ActionResult<SkincareAnalysis>> CreateSkincareAnalysis(SkincareAnalysis analysis)
        {
            var createdAnalysis = await _skincareAnalysisService.CreateSkincareAnalysisAsync(analysis);
            return CreatedAtAction(nameof(GetSkincareAnalysis), new { id = createdAnalysis.Id }, createdAnalysis);
        }

        [HttpPost("analyze")]
        public async Task<ActionResult<SkincareAnalysis>> AnalyzeSkin([FromBody] SkinAnalysisRequest request)
        {
            var analysis = await _skincareAnalysisService.AnalyzeSkinAsync(
                request.UserId, 
                request.ImageUrl, 
                request.Notes);
            return CreatedAtAction(nameof(GetSkincareAnalysis), new { id = analysis.Id }, analysis);
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> UpdateSkincareAnalysis(int id, SkincareAnalysis analysis)
        {
            if (id != analysis.Id)
            {
                return BadRequest();
            }

            await _skincareAnalysisService.UpdateSkincareAnalysisAsync(analysis);
            return NoContent();
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteSkincareAnalysis(int id)
        {
            await _skincareAnalysisService.DeleteSkincareAnalysisAsync(id);
            return NoContent();
        }
    }

    public class SkinAnalysisRequest
    {
        public int UserId { get; set; }
        public string ImageUrl { get; set; }
        public string Notes { get; set; }
    }
}